package edu.nanyang.struts;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import edu.nanyang.domain.User;
import edu.nanyang.hibernate.Login;
import edu.nanyang.util.CommonData;

public class LoginAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String result;
	private String username;
	private String password;
	private String login1;
	private Login login;
	@Override
	public String execute() {
		User s;
		s = login.login(username, password, login1);
		if(s!=null){
			if(CommonData.USER_LOCK_YES.equals(s.getLock())){
				result="登录失败，用户被锁定";
			}else{
				HttpSession  session =ServletActionContext.getRequest().getSession();
				session.setAttribute("userid",s.getId());
				session.setAttribute("user",s);
				result="登录成功";
				System.out.println("登录成功");
			}
		}else{
			result="登录失败，请检查用户名密码或者角色";
		}
		System.out.print(s);
		System.out.println("login1=" + login1);
		System.out.println("username=" + username);
		System.out.println("password=" + password);
		if (s!=null && ("1".equals(login1)) && !CommonData.USER_LOCK_YES.equals(s.getLock())) {
			return SUCCESS;
		} else if (("2".equals(login1)) && s!=null && !CommonData.USER_LOCK_YES.equals(s.getLock())) {
			return "release";
		} else
			return "input";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}
	public String getLogin1() {
		return login1;
	}

	public void setLogin1(String login1) {
		this.login1 = login1;
	}
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
